(function (ns, win, doc) {
ns.Tabs = new Class({
	Extends: ns.Base,
    Binds: ['change', 'check'],
	options: {
        container: '.page-buy',
        labels: '.type li',
        panels: '.sp .ops',
        selectedClass: 'current',
        selectedIndex: 0,
        key: 'gd11x5',
        type: 'Item'
	},
	build: function(ops, self) {
        var container = doc.id(ops.container) || doc.getElement(ops.container),
            labels = container.getElements(ops.labels),
            panels = container.getElements(ops.panels),
            items = [],
            item;
        self.panels = panels[0].getParent();

        labels.each(function (label, i) {
            if (panels[i]) {
                var item = new ns.Tabs[ops.type]({label: label, panel:panels[i], selectedClass: ops.selectedClass, key: ops.key + '_' + i, index: i});
                item.addEvent('focus', self.change);
                if(item.selected){
                    if(items.length > 0) if(self.selectedItem) self.selectedItem.blur();
                    self.select(item);
                    self.fireEvent('change', [item, null], 10);
                }
                items.push(item);
            }
        });
        item = items[ops.selectedIndex];
        if(!self.selectedItem && item) {
            self.select(item);
        }
        self.fireEvent.delay(20, self, ['change', self.selectedItem]);
	},
    observe: function(self, action, actions) {
        self.panels[action]('click', self.check);
    },
	select: function(item){
        var self = this;
		self.selectedItem = item;
		self.selectedItem.focus();
		return self;
	},
	change: function(item){
        var self = this;
		if (item === self.selectedItem) return;
		self.fireEvent('change', [item, this.selectedItem], 20);
		if(self.selectedItem) self.selectedItem.blur();
		self.selectedItem = item;
		return self;
	},
    check: function (e) {
        var self = this,
            el = doc.id(e.target),
            tag = el.get('tag'),
            lastCheck = self.lastCheck;
        if (tag == 'label') {
            el = el.getElement('input');
            tag = el ? 'input' : '';
        }
        if (tag == 'input' && el.checked && el != self.lastCheck) {
            self.fireEvent('change', [self.selectedItem, null]);
            self.lastCheck = el;
        }
    }
});

ns.Tabs.Item = new Class({
	Extends: ns.Base,
    Binds: ['onClick'],
	options: {
		label: '',
		panel: '',
        group: '>span',
        index: 0,
		selected: false/*,
		onFocus: $empty,
		onBlur: $empty*/
	},
	build: function(ops, self) {
        self.selectedClass = ops.selectedClass;
        self.index = ops.index;
		
		self.label = doc.id(ops.label).hideOutline();
        self.panel = doc.id(ops.panel);
		
		if(self.selected = (ops.selected || self.label.hasClass(self.selectedClass))) self.focus(true);

        var name = ops.key,
            groups = self.panel.getElements(ops.group),
            process = function (group, i) {
                group.getElements('label').each(function (label, j) {
                    var id = [name, i, j].join('_');
                    label.set('for', id).getElement('input').set({id: id, name: name/*, value: [i, j].join(',')*/});
                });
            };
        if (!groups.length) {
            process(self.panel, 0);
        } else groups.each(process);
	},
	observe: function(self, action, actions) {
		self.label[action]('click', self.onClick);	
		return self;
	},
	onClick: function(e){
		e && e.preventDefault();
		this.focus();
	},
	focus: function(force){
        var self = this,
            cls = self.selectedClass,
            selected;
		if(self.selected && self.label.hasClass(cls) && !force) return;
		self.selected = true;
		self.label.addClass(cls);
		self.panel.addClass(cls);
        selected = self.panel.getElement('input:checked');
        if (!selected) {
            selected = self.panel.getElement('input').set('checked', true);
        }
		self.fireEvent('focus', self, 20);
		return self;
	},
	blur: function(){
        var self = this,
            cls = self.selectedClass;
		self.selected = false;
		self.label.removeClass(cls);
		self.panel.removeClass(cls);
		self.fireEvent('blur', self, 20);
		return self;
	},
    getValue: function () {
        var self = this,
            selected = self.panel.getElement('input:checked');
        if (!selected) {
            selected = self.panel.getElement('input').set('checked', true);
        }
        return selected.value;
    }
});


})(using('HB'), this, document);
